package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    // 处理预约单查询分页请求 /appointment/list
    @RequestMapping("/list")
    @RequiredPermission(name="预约单查询", expression = "appointment:list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo) {
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses", businesses);

        //业务分类
        List<SystemDictionary> catalogs = systemDictionaryService.queryBySn("business");
        model.addAttribute("catalogs", catalogs);


        return "appointment/list"; // /WEB-INF/views/appointment/list.ftl
    }


    // 处理删除请求 /appointment/delete?id=1
    @RequestMapping("/delete")
    @RequiredPermission(name="预约单删除", expression = "appointment:delete")
    public String delete(Long id) {

        if (id != null) {
            appointmentService.delete(id);
        }
        return "redirect:/appointment/list"; // localhost/appointment/list
    }

    // 处理保存或者修改请求 /appointment/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="预约单编辑", expression = "appointment:saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {

        if (appointment.getId() == null) {
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list"; // localhost/appointment/list
    }


    @RequestMapping("/updateStatus")
    public String updateStatus(Long id, int status) {

        appointmentService.updateStatus(id, status);

        return "redirect:/appointment/list"; // localhost/appointment/list
    }
}
